<?php

/**
 * Created by IntelliJ IDEA.
 * User: jirapatjantsakool
 * Date: 3/7/14
 * Time: 12:24 AM
 */
require('../model/category.php');

require('../db/database.php');
require('../service/common_service.php');

class category_service extends common_service
{
    
    public function findAll()
    {
        global $db;
        $sql = "select * from bf_category";
        $query = $db->query($sql);

        $category_list = array();

        while ($result = $db->fetch_array($query)) {

            $category = new category();

            $category->setId($result['id']);
            $category->setName($result['name']);
            $category->setTitle($result['title']);
            $category->setDescription($result['description']);

            $category_list[] = $category;
        }
        return $category_list;
    }

    public function findCategoryById($id)
    {
        global $db;
        $content = new category();

        $sql = "SELECT "
            . " name, title, description "
            . " FROM bf_category "
            . " WHERE id = ? ";

        if ($stmt = $db->prepare($sql)) {

            //binding param
            mysqli_stmt_bind_param($stmt, 'i', $id);

            $db->execute_statement($stmt);

            $rowMaps = array();
            mysqli_stmt_bind_result($stmt, $rowMaps['name'],
                                           $rowMaps['title'],
                                           $rowMaps['description']);

            //TODO check number record
            if ($db->fetch_statement($stmt)) {

                $content->setId($id);
                $content->setName($rowMaps['name']);
                $content->setTitle($rowMaps['title']);
                $content->setDescription($rowMaps['description']);

            }
            $db->close_connection();
        }
        return $content;
    }
    
    public function delete($category) {
        global $db;
        static $flag = false;
        
        $sql = "DELETE FROM bf_category "
            . " WHERE id = ? ";
        
        if ($stmt = $db->prepare($sql)) {
            //binding param
            mysqli_stmt_bind_param($stmt, 'i', $category->getId()); 
            
            $flag = $db->execute_statement($stmt);
        }
        $db->close_connection();
        return $flag;
    }
    
    public function update($category) {
        global $db;
        static $flag = false;
        
        $sql = "UPDATE bf_category SET "
            . " name = ?, title = ?, description = ? "
            . " WHERE id = ? ";
        
        if ($stmt = $db->prepare($sql)) {
            //binding param
            mysqli_stmt_bind_param($stmt, 'sssi', $category->getName(),
                                                          $category->getTitle(), 
                                                          $category->getDescription(), 
                                                          $category->getId());
            $flag = $db->execute_statement($stmt);
        }
        $db->close_connection();
        return $flag;
    }
    
    public function create($category) 
    {
        global $db;
        static $flag = false;
        
         $sql = "INSERT INTO bf_category"
            . "(name, title, description ) "
            . " VALUES( ?, ?, ?) ";
         

         if ($stmt = $db->prepare($sql)) {
            //binding param
            mysqli_stmt_bind_param($stmt, 'sss', $category->getName(),
                                                          $category->getTitle(), 
                                                          $category->getDescription());
            $flag = $db->execute_statement($stmt);
        }

        $db->close_connection();
         return $flag;
    }

    public function isValidateTokenKey($token)
    {
        global $db;
        $common_service = new common_service();
        return $common_service->validateTokenKey($db, $token, 'token');
    }

}

$category_service = new category_service();
//var_dump($category_service->isValidateTokenKey('abc1234'));

?>
